#Assignment 1
#Multiplication table
import string                                               #importing the string library
def multiplication_table(total):                            #first function
    frontj = len(str(total)) + 1                            #local variable
    lines = 0                                               #local variable
    multiplier = 0                                          #local variable
    topNumber = 0                                           #local variable
    justnum = len(str(total**2)) + 1                        #number to right justify from
    top = " "*frontj + "|"                                  #the start of the top line
    divider = "-"*frontj + "+"                              #starting the divider
    while topNumber < total:                                #loop to create the top and the divider
        topNumber += 1                                      #incriment the numbers to be printed
        numberstr = str(topNumber)                          #converts the int to string
        top = top + str(numberstr).rjust(justnum)           #writes the top line
        divider = divider + "-"*justnum                     #writes the divider
    print top                                               #outputs the top line
    print divider                                           #outputs the dividing line
    for i in range(total):                                  #loop to create the table line by line
        lines += 1                                          #incriments the line number and the first multiplier
        line = str(lines).rjust(frontj) + "|"               #defines the first part of the line
        while multiplier < total:                           #loop to fill in the numbers
            multiplier += 1                                 #incriments the multiplier
            num = str(lines * multiplier).rjust(justnum)    #finds and converts the number to string and justifies it
            line += num                                     #adds the number string to the rest of the line
        print line                                          #prints the line
        multiplier = 0                                      #resets the multiplier
    

def main():                                                             #first function to call
    number = raw_input("Please input a whole number between 0 and 20: ")    #gets the input from the user
    try:                                                                #try function
        number = int(number)                                            #trys to convert input to integer
    except ValueError:                                                  #ValueError exception
        print "invalid input please insert a whole number"              #print error message
        main()                                                          #restarts the program
    if 0 < number <= 20:                                                #checkes if the input is valid
        print "here is your table"                                      #if valid prints a welcome message
        multiplication_table(number)                                    #then starts making the table
    else:                                                               #if the number is over 20 or under 0
        print "print number too large or too small"                     #error message 1
        print "Please try again"                                        #error message 2
        main()                                                          #restarts the program

main()                                                                  #starts the program